<?php
/**
 * Created by PhpStorm.
 * User: alei
 * Date: 11/4/2017 AD
 * Time: 16:56
 */
namespace App\Models\Mysql;

use App\Models\Traits\BaseDBMethod;
use App\Models\Exceptions\DbException;

/**
 * 图片相关模型类
 * Class table_forum_attachment_n
 * @package App\Models\Mysql
 */
class table_n
{
	use BaseDBMethod;
	//数据库连接配置项，见config/database.php
	const DATABASE_CONF = 'lqyw_mysql';

	protected $table = '';
	/**
	 * 根据表名获取DB实例
	 * @param $tname string 表名
	 * @param bool|FALSE $disable_query_log 不启用慢查询日志
	 * @return object
	 */
	protected static function getDBobj($tname, $disable_query_log=FALSE)
	{
		$connObj = self::getConnObj();
		if(FALSE === $disable_query_log) {
			return $connObj->table($tname);
		} elseif(TRUE === $disable_query_log) {
			$connObj->disableQueryLog();
			return $connObj->table($tname);
		}
	}

	public function fetch_all_by_id($tableid, $idtype, $ids, $orderby = '',
									$isimage = false, $isprice = false,
									$remote = false, $limit = false)
	{
		if($this->check_id($idtype, $ids)) {
			$attachments = array();
			if($orderby) {
				$orderby = 'ORDER BY '.$orderby;
			}
			$idtype_sql = is_array($ids) ? (' AND `'.$idtype.'` IN :ids') :
				(is_string($ids) ? (' AND `'.$idtype.'`=:ids') : '');
			$isimage = $isimage === false ? '' : ' AND `isimage`=:isimage';
			$isprice = $isprice === false ? '' : ' AND `price`>:price';//0
			$remote = $remote === false ? '' : ' AND `remote`=:remote';
			$limit = $limit < 1 ? '' : self::limit(0, $limit);
			$result = self::getConnObj()->select(
				'SELECT * FROM '.$this->get_table($tableid).' WHERE '.
				$idtype_sql.$isimage.$isprice.$remote.$orderby.$limit, [
					'ids' => $ids, 'isimage' => $isimage,
					'price' => 0, 'remote' => $remote
				]
			);
			foreach($result as &$value) {
				$attachments[$value->aid] = $value;
			}
			return $attachments;
		} else {
			return array();
		}
	}

	protected function check_id($idtype, $ids)
	{
		if($idtype == 'pid' && $this->table == 'forum_attachment_unused') {
			return false;
		}
		if(in_array($idtype, array('aid', 'tid', 'pid', 'uid')) && !empty($ids)) {
			return true;
		} else {
			return false;
		}
	}

	protected function get_table($tableid)
	{
		if(! is_numeric($tableid)) {
			list($idtype, $id) = explode(':', $tableid);
			if($idtype == 'aid') {
				$aid = dintval($id);
				$temp = self::result_first(
					'SELECT tableid FROM `pre_forum_attachment` WHERE aid=?',
					[$aid]
				);
				if($temp) $tableid = $temp->tableid; else $tableid = -1;
			} elseif($idtype == 'tid') {
				$tid = (string)$id;
				$tableid = dintval($tid{strlen($tid)-1});
			} elseif($idtype == 'pid') {
				$pid = dintval($id);
				$temp = self::result_first(
					'SELECT tableid FROM `pre_forum_attachment` WHERE pid=? LIMIT 1',
					[$pid]
				);
				if($temp) $tableid = $temp->tableid; else $tableid = -1;
				$tableid = $tableid >= 0 && $tableid < 10 ? intval($tableid) : 127;
			}
		}
		if($tableid >= 0 && $tableid < 10) {
			return 'forum_attachment_'.intval($tableid);
		} elseif($tableid == 127) {
			return 'forum_attachment_unused';
		} else {
			throw new DbException('Table forum_attachment_'.$this->table.' has not exists');
		}
	}

} //! end PreCtUi
